﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AppEnvironment.Lib
{
    public class Presenter
    {
        public void ViewInitialized(bool isFirstTime)
        {
            if (isFirstTime)
            {
                if (!Navigation.IsReturning())
                {
                    ClearState();
                }
            }
            Init(isFirstTime);
        }


        public void ViewAboutToRender(bool isFirstTime)
        {
            AboutToRender(isFirstTime);
        }


        protected virtual void Load(bool isFirstTime)
        {
            //empty
        }

        /// <summary>
        /// Override to clear any state.
        /// If not overridden, and your instance is stateful, then the state will not get cleared on reload.
        /// </summary>
        protected virtual void ClearState()
        {
            //empty
        }

        public void ViewLoaded(bool isFirstTime)
        {
            Load(isFirstTime);
        }

        protected virtual void Init(bool isFirstTime)
        {
            //empty
        }

        protected virtual void AboutToRender(bool isFirstTime)
        {
            //empty
        }

        /// <summary>
        /// This simulates an inital page load
        /// </summary>
        public void Run()
        {
            ViewInitialized(true);
            ViewLoaded(true);
            ViewAboutToRender(true);
        }

        /// <summary>
        /// This simulates a page postback, after inital load
        /// </summary>
        public void RunPostBack()
        {
            ViewInitialized(false);
            ViewLoaded(false);
            ViewAboutToRender(false);
        }
    }
}
